<?php

m2import('phpinchains/psp/Tag.php');
m2import('phpinchains/tagsupport/CmsImage.php');

class cms_imageHandler extends phpinchains_psp_Tag
{

    /**
     * Is a body tag (if false, body is ignored)
     */
    var $tagType = PSP_EMPTY_TAG;
    /**
     * Tag name in form namespace:tagname
     */
    var $tagName   = 'cms:image';

    var $requiredAttributes = array(
        'id' => '(imageid:\d+|\d+)'
    );
    var $attributes = array(
        'var',
        'width',
        'height',
        'mode',
        'alttext',
        'map',
        'attributes'
    );

    function doStartTag()
    {
        $mode = $this->getAttribute('mode')?$this->getAttribute('mode'):'normal';
        if (preg_match('/imageid:(\d+)/', $this->getAttribute('id'), $matches)) {
            $id = $matches[1];
        } else {
            $id = $this->getAttribute('id');
        }

        $cmsimg = new CmsImage($id, $this->getAttribute('width'), $this->getAttribute('height'));

        if ($mode == 'tag')
            $value = $cmsimg->getTag($this->getAttribute('alttext'), $this->getAttribute('map'), $this->getAttribute('attributes'));
        else
            $value = $cmsimg->getUrl();
        if ($this->getAttribute('var')) {
            $this->actionform[$this->getAttribute('var')] = $value;
        } else {
            $this->out->write($value);
        }
        return PSP_SKIP_BODY;
    }

    // In empty tag only necessary, if tag skips page, e.g. redirect -> no further processing needed
    function doEndTag()
    {
        //return PSP_SKIP_PAGE;
    }

}
?>